var app = getApp();
Page({
    data:{
        curPage : "/zofui_sales/park/pay",
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        params : {},
        info : [],
		inited : false,
        userinfo : {},
        isjfdk : true,
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : (options.scene ? options.scene : 0);
        self.data.options = options;
        
        app.com.auth(function(user){
            var postdata = {
                op : 'payinfo',
                zfid : zfid,
            };
            app.com.http('park','POST',postdata,0,false,'','',function(res){
                if(res.data && !res.data.errno){

                    var data = res.data.data;
                    self.setData({
                       params : res.data.data.params,
                       info : res.data.data.info,
                       inited : true,
                    });
                    
                    wx.setNavigationBarTitle({
                        title:  '支付',
                    });
                }else{
                    app.com.alert(res.data.message,function(){
                        wx.navigateBack();
                    });
                }
            });
        });
    },
    onPullDownRefresh: function () {
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    changedk(){
        if(this.data.info.canjifendis > 0){
            this.setData({
                isjfdk : !this.data.isjfdk
            })
        }
    },
    authed(e){
        var self = this;
        self.setData({
            'params.isneedauth' : 0,
        })
        self.subpay();
    },
    newfn(){
        this.setData({
            isnew : !this.data.isnew
        })
    },
    setact(e){
        var index = e.currentTarget.dataset.index*1;
        this.setData({
           actindex : index,
           isauto : 0,
           tabtye : index >= 1 ? 2 : 1,
           iskeyword : true,
        })
    },
    subpay(){
        var self = this;

        if(this.data.params.needauth == 1 && !this.data.isauthed){
            this.selectComponent('#loginauth').hideauth();
            return false;
        }
        var postdata = {
            op:'payit',
            oid:self.data.info.orderid,
            jfdk : self.data.isjfdk ? self.data.info.canjifendis : 0,
        };
        app.com.http('park','POST',postdata,0,true,'','',function(res){
            if (res.data  && !res.data.errno) {

                if(res.data.data.payed == 1){
                    app.com.alert('已支付完成',function(){
                        wx.navigateBack();
                    });
                    return false;
                }

                wx.requestPayment({
                    appId : res.data.data.appId,
                    timeStamp : res.data.data.timeStamp,
                    nonceStr : res.data.data.nonceStr,
                    package : res.data.data.package,
                    signType : 'MD5',
                    paySign : res.data.data.paySign,
                    success : function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            app.com.alert('已支付完成',function(){
                                wx.navigateBack();
                            });
                        }
                    },
                    fail: function (payres) {
                        console.log(payres);
                    }
                });

            }else if(res.data.errno == 2){
                app.com.confirm(res.data.message,function(){
                    var tourl = '/zofui_sales/ucard/buycard';
                    wx.navigateTo({
                      url: tourl
                    });
                },false,'取消','成为会员');
                
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

})